import shortuuid
from sqlalchemy.orm import session

from database import db
from ..models.brand import Brand

class BrandService(object):

    @classmethod
    def get_brand_by_id(cls,brand_id):
        return Brand.query.filter(Brand.id == brand_id).first()

    @classmethod
    def create_brand(cls,brand_name,describe,logo,**kwargs):
        kwargs.update({
            'id':shortuuid.uuid(),
            'brand_name':brand_name,
            'describe':describe,
            'logo':logo

        })
        brand = Brand(**kwargs)
        db.session.add(brand)
        db.session.commit()
        return db

    @classmethod
    def get_brand_by_name(cls,brand_name):
        query = Brand.query.filter(Brand.status != Brand.REMOVE_STATUS)
        return query.filter(Brand.brand_name.like('%{}%'.format(brand_name))).all()

    @classmethod
    def get_brand_all(cls):
        return Brand.query.filter(Brand.status!=Brand.REMOVE_STATUS).all()

    @classmethod
    def update_brand(cls,id,brand_name,describe,logo,status):
        return db.session.query(Brand).filter(Brand.id==id). \
                update(
                    {
                        Brand.brand_name:brand_name,
                        Brand.describe:describe,
                        Brand.logo:logo,
                        Brand.status:status
                    }

                )

